// 引入接口文件
import { queryAllAddress, deleteLiveServ, downLivePerson,queryByLiveBroadcastId } from './liveListServ.js'

// 对象和JSON字符串互转
import { parse } from 'qs'
// 弹出消息框
import { message } from 'antd'
// 日期处理对象
import moment from 'moment'
/* isEmpty 对象是否为空
 * isArray 对象是否数组
 * cloneDeep 对象深拷贝
 */
import { isEmpty, isArray, cloneDeep, merge } from 'lodash'
// 引入路由对象
import { hashHistory } from 'dva/router'
import { calculate_object_name } from 'utils/upload'
import { download, makeAddress } from "utils/util"
import { generateQrCode } from "../../../systemAdmin/gameConfig/gameConfigList/gameConfigListServ";

// 初始默认状态
const defultState = {
	exportLoading: false,
	loading: false,
	areaList: [],
	checkBtn: true,
	btnLoading: false,
	actionType: '',
	biz: {
		queryForm: {}
	},
	modalObj: {
		visible: false,
	},
	codeList: {}, //验证码返回数据
	codeQuery: {}, //验证码
	livePersonVisible: false, //是否显示观看人数列表模态框
	liveId: "", //直播ID
	liveStreamingModel: false,
	liveStreamingData: []
}

// 声明module
const tmpModule = {
	// 默认状态
	state: cloneDeep(defultState),
	// 入口函数(先执行)
	subscriptions: {
		setup({ dispatch, history }) {
			history.listen(({ pathname, query }) => {
				// 页面路由匹配时执行
				if ('' + tmpModule.pathname === '' + pathname) {
					// 执行业务操作
					// dispatch({
					// 	type: 'queryInfo',
					// 	payload: {}
					// }) // end dispatch
					dispatch({ type: 'getAreaList', payload: {} })
				} // end if
			}) // end listen
		},
	},

	// 方法列表
	effects: {

		//获取级联列表
		* getAreaList({ payload }, { put, call, select }) {
			try {
				const { data, success } = yield call(queryAllAddress);
				if (success) {
					let areaList = [];
					let arr = [data[0]]
					let attr = [data[data.length - 1]]
					data[0] = {
						label: "北京",
						value: "110100",
						children: arr
					}
					data[data.length - 1] = {
						label: "澳门",
						value: "820000",
						children: attr
					}
					//去掉第三级，
					areaList = makeAddress(data, 2);

					yield put({ type: 'updateStore', payload: { areaList: areaList } });
				}
			} catch (e) {

			}
		},


		* deleteLive({ payload }, { put, call, select }) {
			try {
				let state = yield select(state => state[tmpModule.namespace])
				const { data, success, msg } = yield call(deleteLiveServ, payload)
				if (success) {
					message.success('操作成功')
					state.biz.refreshListDatas(true)
				}
			} catch (e) {
				console.error(e)
			}
		},

		* downLivePerson({ payload }, { put, call, select }) {
			try {
				//   let tmpState = yield select(state => state[tmpModule.namespace])
				//   let {queryForm} = tmpState.biz
				const { data, success } = yield call(downLivePerson, payload)
				yield put({ type: 'updateStore', payload: { exportLoading: false } })
				if (success) {
					// yield put({ type: 'updateStore', payload: {  data } });
					if (data) {
						download(data);
						message.success('导出成功');
					} else {
						message.error('导出失败');
					}
					// window.open(data.url)

				}
			} catch (e) {
				console.error(e)
			}
		},
		* generateQrCode({ payload }, { put, call, select }) {
			try {

				yield put({ type: 'updateStore', payload: { imgLoading: true } })
				let data1 = yield call(generateQrCode, { ...payload, channelType: 0, channelWxacode: 'live' })

				// let data2 = yield call(generateQrCode, {...payload,channelType:1,channelWxacode:'live'})
				if (data1.success) {
					if (data1.msg) {
						yield put({ type: 'updateStore', payload: { zmzjCode: data1.msg } })
					} else {

					}
				}
				// if(data2.success){
				// 	if(data2.msg){
				// 		yield put({ type: 'updateStore', payload: { zysqCode:data2.msg} })
				// 	}else{

				// 	}
				// }
				yield put({ type: 'updateStore', payload: { imgLoading: false } })

			} catch (e) {

			}
		},
		// 根据直播ID获取直播视频列表
		*queryByLiveBroadcastId({ payload }, { put, call, select }) {
			const { result, success } = yield call(queryByLiveBroadcastId, payload)
			if(success){
				console.log('列表',result)
				yield put({ type: 'updateStore', payload: { liveStreamingData: result } })
			}
		}
	},


	// 存入状态机
	reducers: {
		updateStore(preState, action) {
			return Object.assign({}, preState, action.payload)
		},
	}
}

export default tmpModule